//
//  NSDictionary+Element.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSDictionary+Element.h"

@implementation NSDictionary (Element)


+ (id)getElementForKey:(id)key fromDict:(NSDictionary *)dict forClass:(Class)forClass
{
    if(![dict isKindOfClass:[NSDictionary class]])
    {
        return nil;
    }
    
    id obj = [dict objectForKey:key];
    
    if ([obj isKindOfClass:forClass])
    {
        if (([obj isKindOfClass:[NSString class]] && [obj isEqual:@""]) || [obj isKindOfClass:[NSNull class]])
        {
            return nil;
        }
        else
        {
            return obj;
        }
    }
    else
    {
        if([obj isKindOfClass:[NSNumber class]])
        {
            if([NSStringFromClass(forClass) isEqualToString:NSStringFromClass([NSString class])])
            {
                return [NSString stringWithFormat:@"%@",obj];
            }
            else
            {
                
            }
        }
        else if([obj isKindOfClass:[NSString class]])
        {
            if([NSStringFromClass(forClass) isEqualToString:NSStringFromClass([NSNumber class])])
            {
                NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
                NSNumber *number = [f numberFromString:obj];
                return number;
            }
            else
            {
                
            }
        }
        else
        {
            
        }
        
    }
    return nil;
}
@end

